<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\StoreItem;
use App\Models\Item;
use App\Models\Pack;
use App\Models\PreferCate;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ItemController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function __construct()
    {
        view()->share([
            '_hd' => 'am-in',
            '_item' => 'am-active',
        ]);

    }


    public function index(Request $request)
    {

        $where = function ($query) use ($request) {

            if($request->has('name')and $request->name !=''){
                $search = "%".$request->name."%";
                $query->where('name','like',$search);
            }

            if($request->has('pack_id')and $request->pack_id !='-1'){

                $query->where('pack_id',$request->pack_id);
            }
        };
        $cates=  PreferCate::with('packs')->orderBy('sort')->get();
        $items=Item::where($where)->with('pack')->orderBy('sort')->paginate(20);


        return view('admin.item.index',compact('items','cates'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {

      $cates=  PreferCate::with('packs')->orderBy('sort')->get();

        return view('admin.item.create',compact('cates'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreItem $request)
    {
      $data['pack_id']=$request->pack_id;
      $data['name']=$request->name;
      $info=Item::where($data)->first();
      //防止一个套餐中有相同名字的项目
      if($info){
          return back()->with('success','所选套餐中已有当前项目');
      }
      Item::create($request->all());
      return redirect('/admin/item')->with('success','新增成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $item=Item::find($id);
        $cates=  PreferCate::with('packs')->orderBy('sort')->get();
        return view('admin.item.edit',compact('cates','item'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $item=Item::find($id);
       $data['pack_id']=$request->pack_id;
        $data['name']=$request->name;
       $info=Item::where($data)->first();
       //修改为所属套餐时，防止修改后的套餐有当先项目名字
       if($request->pack_id != $item->pack_id&&isset($info)){
           return back()->with('success','所修改套餐已有当前项目');
       }
       $item->update($request->all());
       return redirect('/admin/item')->with('success','修改成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Item::destroy($id);
        return back()->with('success','删除成功');
    }

    public function sort(Request $request){
        $item = Item::find($request->id);
        $item->sort = $request->sort;
        $item->save();
    }
}
